package mail;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.SpringLayout;

import mail.gui.AccountPanel;
import mail.gui.ListPanel;
import mail.gui.MailPanel;

public class MultiEmail extends JFrame {

  /**
   * Launch the application
   * @param args
   */
  public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          MultiEmail frame = MultiEmail.getInstance();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
  
  private AccountPanel accountPanel = null;
  private ListPanel listPanel = null;
  private MailPanel mailPanel = null;
  private static MultiEmail instance = null;
  
  synchronized public static MultiEmail getInstance() {
    if(instance == null) {
      instance = new MultiEmail();
    }
    return instance;
  }
  

  public AccountPanel getAccountPanel() {
    return accountPanel;
  }


  public void setAccountPanel(AccountPanel accountPanel) {
    this.accountPanel = accountPanel;
  }


  public ListPanel getListPanel() {
    return listPanel;
  }


  public void setListPanel(ListPanel listPanel) {
    this.listPanel = listPanel;
  }


  public MailPanel getMailPanel() {
    return mailPanel;
  }


  public void setMailPanel(MailPanel mailPanel) {
    this.mailPanel = mailPanel;
  }


  /**
   * Create the frame
   */
  private MultiEmail() {
    super();
    setTitle("MultiEmail");
    getContentPane().setLayout(new BorderLayout());
    setBounds(100, 100, 800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setName("MultiEmail");
    
    accountPanel = new AccountPanel(this);
    getContentPane().add(accountPanel, BorderLayout.WEST);

    JPanel emailPanel = new JPanel();
    emailPanel.setLayout(new BorderLayout());
    getContentPane().add(emailPanel, BorderLayout.CENTER);

    listPanel = new ListPanel(this);
    emailPanel.add(listPanel, BorderLayout.NORTH);

    mailPanel = new MailPanel(this);
    emailPanel.add(mailPanel, BorderLayout.SOUTH);

    JEditorPane editorPanel;
    editorPanel = new JEditorPane();
    editorPanel.setText("hello all");

  	JMenuBar menuBar = new JMenuBar();
  	setJMenuBar(menuBar);

  	JMenu fileMenu = new JMenu();
  	fileMenu.setText("File");
  	menuBar.add(fileMenu);

  	JMenuItem openMenu = new JMenuItem();
  	openMenu.setText("Open");
  	fileMenu.add(openMenu);
  	
  	JMenuItem newMailMenu = new JMenuItem();
  	newMailMenu.setText("New Mail");
  	newMailMenu.addActionListener(new ActionListener() {
  	  public void actionPerformed(ActionEvent e) {
  	    if(getAccountPanel().getAcct() == null || getAccountPanel().getAcct().getAccountName() == null) {
  	      JOptionPane.showMessageDialog(MultiEmail.this, "Please select an email account");
  	      return;
  	    }
  	    MailEditor editor = new MailEditor(getAccountPanel().getAcct());
  	    editor.setVisible(true);
  	  }
  	});
    fileMenu.add(newMailMenu);
    
    JMenuItem saveAccount = new JMenuItem();
    saveAccount.setText("Save Accounts");
    saveAccount.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(AccountManager.saveAccount(MultiEmail.this.getAccountPanel().getAccountList())) {
          JOptionPane.showMessageDialog(MultiEmail.this, "Save Accounts successfully");
        }else {
          JOptionPane.showMessageDialog(MultiEmail.this, "Save Accounts failed");
        }
      }
    });
    fileMenu.add(saveAccount);

    //
  }

}
